package com.zhuoyue.biz.core.command.impl.curtains;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.CmdParams;
import com.zhuoyue.biz.core.command.model.SilentCurtainCmd;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>Title: 静音窗帘<／p>
 * <p>Description: <／p>
 * <p>Company: JiZi<／p>
 *
 * @author wjming
 * @version 1.0
 * @date 2018年12月10日
 */
public class SilentCurtain0Fe60209 {
    /**
     * 开关停控制cmd模型
     */
    private static final String CMD_MODEL = "0701${gatewayMac}${deviceMac}1655AAAA55${checkSums}020A0A00000001${status}0001010100000000";
    /**
     * 百分比控制模型
     */
    private static final String PERCENT_MODEL = "0701${gatewayMac}${deviceMac}1655AAAA55${checkSums}22090A0A0000000100${status}01010100000000";
    /**
     * 设备状态检查
     */
    private static final String CHECK_MODEL = "0701${gatewayMac}${deviceMac}1655AAAA552E22010A0A00000001000001010100000000";

    private static final String OPEN = "01";
    private static final String CLOSE = "00";
    private static final String STOP = "02";
    private static final String OPEN_PARAM = "OPEN";
    private static final String CLOSE_PARAM = "CLOSE";
    private static final String STOP_PARAM = "STOP";
    /**
     * 开校验和
     */
    private static final String OPEN_CHECK_SUMS = "3022";
    /**
     * 关校验和
     */
    private static final String CLOSE_CHECK_SUMS = "2F22";
    /**
     * 停止校验和
     */
    private static final String STOP_CHECK_SUMS = "3122";


    public static String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";
        if (CmdParams.ControlParam.SWITCH_STOP.equals(controlType)) {
            cmd = getSwitchStopCmd(map);
        } else if (CmdParams.ControlParam.PERCENT.equals(controlType)) {
            cmd = getPercentCmd(map);
        } else if (CmdParams.ControlParam.CHECK.equals(controlType)) {
            cmd = getCheckCmd(map);
        }

        return cmd;

    }

    /**
     * 设备检查
     *
     * @param map
     * @return
     */
    private static String getCheckCmd(Map<String, String> map) {
        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        return new SilentCurtainCmd(gatewayMac, deviceMac).getCmd(CHECK_MODEL);
    }


    /**
     * 百分比控制
     *
     * @param map
     * @return
     */
    private static String getPercentCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        Integer value = Integer.parseInt(actionsMap.get("value"));

        String percent = CmdUtils.intToHexTwo(value);
        String checkSum = CmdUtils.intToHexTwo(54 + value);

        return new SilentCurtainCmd(gatewayMac, deviceMac, checkSum, percent).getCmd(PERCENT_MODEL);

    }

    /**
     * 开关停控制
     *
     * @param map
     * @return
     */
    private static String getSwitchStopCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String state = actionsMap.get("state");

        if (OPEN_PARAM.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, OPEN_CHECK_SUMS, OPEN).getCmd(CMD_MODEL);
        } else if (CLOSE_PARAM.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, CLOSE_CHECK_SUMS, CLOSE).getCmd(CMD_MODEL);
        } else if (STOP_PARAM.equals(state)) {
            return new SilentCurtainCmd(gatewayMac, deviceMac, STOP_CHECK_SUMS, STOP).getCmd(CMD_MODEL);
        }

        return null;
    }


}
